// pages/detailAddress/detailAddress.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        setAddressInfo: { //设置的地址
            linkman: "", //联系人
            phone: "",//手机号码
            province: "北京市",//保存省
            city: "北京市", //保存市
            district: "东城区",//保存区
            street: "",//街道门牌信息
            checked: false
        },
        show: false,//点击保存的时候，控制弹出层的显示和隐藏
        text: null,//弹出层的提示信息
        index: 0,//获取点击编辑的商品的下标
        type: "add"//判断是从点击编辑跳转的这个页面还是从点击添加地址跳转的,add表示点击添加地址跳转的，edit表示点击编辑跳转的
    },



    // 选择地区
    bindRegionChange(e) {
        this.setData({
            'setAddressInfo.province': e.detail.value[0],
            "setAddressInfo.city": e.detail.value[1],
            "setAddressInfo.district": e.detail.value[2],
        })
    },
    // 获取输入的联系人信息
    getLinkMan(e) {
        this.setData({
            'setAddressInfo.linkman': e.detail.value
        })
    },
    // 获取输入的手机号码
    getPhone(e) {
        this.setData({
            'setAddressInfo.phone': e.detail.value
        })
    },
    // 获取输入的详细地址
    getStreet(e) {
        this.setData({
            'setAddressInfo.street': e.detail.value
        })
    },

    // 点击保存
    saveSetAddress() {
        if (!this.data.setAddressInfo.linkman) {
            this.setData({
                text: '姓名不能为空'
            })
            this.setData({ show: true })
            setTimeout(() => {
                this.setData({
                    show: false
                })
            }, 1200)
        } else if (!this.data.setAddressInfo.phone) {
            this.setData({
                text: '手机号不能为空'
            })
            this.setData({ show: true })
            setTimeout(() => {
                this.setData({
                    show: false
                })
            }, 1200)
        } else if (!this.data.setAddressInfo.street) {
            this.setData({
                text: '请输入详细地址'
            })
            this.setData({ show: true })
            setTimeout(() => {
                this.setData({
                    show: false
                })
            }, 1200)
        } else {
            if (this.data.type == "add") {
                let setAddressInfoArr = wx.getStorageSync('setAddressInfoArr') || [];
                let addressInfo = this.data.setAddressInfo;
                let result = setAddressInfoArr.find(item => {
                    return item.linkman == addressInfo.linkman;
                })
                if (!result) {
                    setAddressInfoArr.push(addressInfo)
                    wx.setStorageSync('setAddressInfoArr', setAddressInfoArr)
                    this.setData({
                        show: true
                    })
                    setTimeout(() => {
                        this.setData({
                            show: false
                        })
                        wx.navigateTo({
                            url: '/pages/address/address',
                        })
                    }, 900)

                }
            } else if (this.data.type == "edit") {
                let index = this.data.index;
                let addressInfo = this.data.setAddressInfo;
                let setAddressInfoArr = wx.getStorageSync('setAddressInfoArr');
                setAddressInfoArr[index] = addressInfo;
                wx.setStorageSync('setAddressInfoArr', setAddressInfoArr)
                wx.navigateTo({
                    url: '/pages/address/address',
                })
            }

        }

        // let index = this.data.index;
        // console.log(index)

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //点击编辑跳转到添加收货地址页面 
        if (options.type == "edit") {
            let index = options.key;
            let address = JSON.parse(options.addressArr)[index];
            this.setData({
                setAddressInfo: {
                    linkman: address.linkman,
                    phone: address.phone,
                    province: address.province,
                    city: address.city,
                    district: address.district,
                    street: address.street
                },
                index,
                type: "edit"
            })
        } else if (options.type == "add") {
            this.setData({
                type: 'add'
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})